﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Edgar.Entities.MasterData;
using Edgar.Frontend.Wpf.Events;

namespace Edgar.Frontend.Wpf
{
    /// <summary>
    /// Interaction logic for AddSkillDialog.xaml
    /// </summary>
    public partial class AddSkillDialog : Window
    {
        private readonly Rulebook _rulebook;

        public AddSkillDialog(Rulebook rulebook)
        {
            InitializeComponent();

            CategoryComboBox.ItemsSource = new[] { 
                new { Text = "Wissensfertigkeit", Value = "KnowledgeSkill" },
                new { Text = "Kunstfertigkeit", Value = "CraftingSkill" },
                new { Text = "Allgemeine Fertigkeit", Value = "GeneralSkill" }
            };

            _rulebook = rulebook;
        }

        public string SelectedCategory
        {
            get
            {
                return (string)CategoryComboBox.SelectedValue;
            }
        }

        public Skill SelectedSkill
        {
            get
            {
                if (SkillComboBox.SelectedItem != null) return ((Skill)SkillComboBox.SelectedItem);
                if (!string.IsNullOrWhiteSpace(SkillComboBox.Text) &&
                    !string.IsNullOrWhiteSpace(SelectedCategory))
                {
                    switch (SelectedCategory)
                    {
                        case "KnowledgeSkill": return new KnowledgeSkill { Name = SkillComboBox.Text };
                        case "CraftingSkill": return new CraftingSkill { Name = SkillComboBox.Text };
                        default: throw new NotSupportedException("Only WAH and CHA custom skills supported!");
                    }
                }
                return null;
            }
        }
        
        private void FillSkillsComboBox(string category)
        {
            switch (category)
            {
                case "KnowledgeSkill":
                    SkillComboBox.ItemsSource = _rulebook.Skills.KnowledgeSkills;
                    break;
                case "CraftingSkill":
                    SkillComboBox.ItemsSource = _rulebook.Skills.CraftingSkills;
                    break;
                case "GeneralSkill":
                    SkillComboBox.ItemsSource = _rulebook.Skills.GeneralSkills;
                    break;
            }
        }

        private void SkillComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectedCategory == "GeneralSkill" && SelectedSkill != null) AttributeLabel.Content = SelectedSkill.Attribute;
        }

        private void CategoryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SkillComboBox.IsEditable = SelectedCategory != "GeneralSkill";
            SkillComboBox.Text = null;
            if (SelectedCategory == "KnowledgeSkill") AttributeLabel.Content = "Wahrnehmung";
            else if (SelectedCategory == "CraftingSkill") AttributeLabel.Content = "Charisma";
            else if (SelectedCategory == "GeneralSkill") AttributeLabel.Content = string.Empty;
            FillSkillsComboBox(SelectedCategory);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (SelectedSkill == null) return;

            if (SkillAdded != null)
                SkillAdded(this, new SkillEventArgs { Name = SelectedSkill.Name, Category = SelectedSkill.Category });
            Close();
        }

        public event EventHandler<SkillEventArgs> SkillAdded;
    }
}
